package com.cn.goodsadmin.service;

import com.cn.goodsadmin.common.frame.NetService;
import com.cn.goodsadmin.common.frame.RequestInfo;
import com.cn.goodsadmin.common.observes.MessageConfig;
import com.cn.goodsadmin.common.observes.MessageManager;
import com.cn.goodsadmin.config.BaseConfig;
import com.cn.goodsadmin.config.CgiConfig;
import com.cn.goodsadmin.observer.UpdateFixtimeMessage;
import com.cn.goodsadmin.po.Action;
import com.cn.goodsadmin.po.Balance;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("actionService")
public class ActionService extends BaseService{

    public void addAction(Action action) throws Exception{
        actionDao.save(action);
    }

    public List<Action> getAllAction(int appid) throws Exception{
        return actionDao.findTotalAction(appid);

    }

    public List<Balance>  getAllBalance() throws Exception{
       return  balanceDao.getAllBalance();
    }

    public void  updateAction(Action action)throws Exception{
        actionDao.update(action);
    }

    public void deleteAction(Action action) throws Exception{
        actionDao.delete(action);
    }

    public void updateFixTime(Action action){
        if(action.getcBalance()== BaseConfig.FIXTIMECOFFEEACTION){
            int appid=action.getcAppid();
            RequestInfo requestInfo=new RequestInfo();
            requestInfo.url= BaseConfig.updatefixtimeUrl;
            requestInfo.appId=String.valueOf(appid);
            requestInfo.requestKey= CgiConfig.UPDATEFIXTIME;
            NetService.getInstance().startAsyHttpNetWork(requestInfo);
        }
    }

    public void sendUpdateFixtimeMessage(Action action){
        UpdateFixtimeMessage message=new UpdateFixtimeMessage(MessageConfig.FIXTIMEACTIONUPDATE,action);
        MessageManager.getInstanceMessage().putMessage(message);
    }
}
